package cn.gdrj.est.service.impl;

import cn.gdrj.est.constants.Constants;
import cn.gdrj.est.dao.PayOrderDao;
import cn.gdrj.est.dto.PayOrderDTO;
import cn.gdrj.est.entity.PayOrder;
import cn.gdrj.est.service.PayOrderService;
import cn.gdrj.est.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * TODO(这里用一句话描述这个类的作用)
 *
 * @author jun
 * @date 2021/11/16
 */
@Service
public class PayOrderServiceImpl implements PayOrderService {
    @Autowired
    private PayOrderDao payOrderDao;
    @Override
    public int savePayOrder(PayOrderDTO payOrderDTO) {
        PayOrder payOrder = BeanUtils.convertToDTO(payOrderDTO, PayOrder.class);
        payOrder.setId(null);
        payOrder.setCreatedTime(new Date());
        payOrder.setIsDelete(Constants.EST_NOT);
        payOrder.setStatus(Constants.Order.PayStatus.UN_PAY);
        payOrder.setCreatedUserId(payOrderDTO.getUserId());

        int count= payOrderDao.insertSelective(payOrder);
        payOrderDTO.setId(payOrder.getId());

        return count;
    }

    @Override
    public PayOrderDTO getByPayNo(String payNo) {
        return payOrderDao.selectByPayNo(payNo);
    }

    @Override
    public int updateOrderStatusToSuccess(Long id, String tradeNo) {
        PayOrder payOrder = new PayOrder();
        payOrder.setId(id);
        payOrder.setAliTradeNo(tradeNo);
        payOrder.setStatus(Constants.Order.PayStatus.SUCCESS);
        payOrder.setUpdatedTime(new Date());
        return payOrderDao.updateByPrimaryKeySelective(payOrder);
    }
}
